<template>
  <el-dialog
    :model-value="visible"
    @update:model-value="updateVisible"
    title="查看"
    width="1200px"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <el-descriptions :column="1" border class="custom-descriptions">
      <el-descriptions-item label="流程名称">
        {{ processData.templateName || '-' }}
      </el-descriptions-item>
      <el-descriptions-item label="创建人">
        {{ processData.creatorName || '-' }}
      </el-descriptions-item>
      <el-descriptions-item label="创建时间">
        {{ parseTime(processData.createTime) || '-' }}
      </el-descriptions-item>
      <el-descriptions-item label="最近一次修改时间">
        {{ parseTime(processData.updateTime) || '-' }}
      </el-descriptions-item>
      <el-descriptions-item label="版本">
        <el-select v-model="selectedVersion" @change="handleVersionChange" style="width: 120px; margin: 20px 0">
          <el-option
            v-for="version in versions"
            :key="version.value"
            :label="version.label"
            :value="version.value"
          />
        </el-select>
        <div class="flow-diagram" ref="flowDiagram">
          <div class="flow-nodes">
            <!-- 开始节点 -->
            <div class="flow-node start-node">
              <div class="node-content">开始</div>
              <!-- 开始节点后的箭头 -->
              <div class="node-arrow" v-if="processData.nodes && processData.nodes.length > 0"></div>
            </div>

            <!-- 中间节点 -->
            <div
              v-for="(node, index) in processData.nodes || []"
              :key="node.nodeId"
              class="flow-node process-node"
              :class="{ active: selectedNodeId === node.nodeId }"
              @click="handleNodeClick(node)"
            >
              <div class="node-content">{{ node.nodeName }}</div>
              <!-- 节点后的箭头 -->
              <div class="node-arrow"></div>
            </div>

            <!-- 结束节点 -->
            <div class="flow-node end-node">
              <div class="node-content">结束</div>
            </div>
          </div>
        </div>
        
        <!-- 节点信息弹窗 -->
        <el-dialog
          v-model="nodeDetailVisible"
          title="节点详情"
          width="600px"
          :close-on-click-modal="false"
        >
          <div v-if="selectedNode" class="node-detail-content">
            <el-descriptions :column="1" border class="custom-descriptions">
              <el-descriptions-item label="节点名称">
                {{ selectedNode.nodeName || '-' }}
              </el-descriptions-item>
              <el-descriptions-item label="审批人类型">
                {{ formatApproverType(selectedNode.approverType) || '-' }}
              </el-descriptions-item>
              <el-descriptions-item label="审批角色">
                {{ selectedNode.approverValue || '-' }}
              </el-descriptions-item>
            </el-descriptions>
            <!-- <div class="info-row">
              <span class="label">节点名称：</span>
              <span class="value">{{ selectedNode.nodeName }}</span>
            </div>
            <div class="info-row">
              <span class="label">审批人类型：</span>
              <span class="value">{{ formatApproverType(selectedNode.approverType) }}</span>
            </div>
            <div class="info-row">
              <span class="label">审批角色：</span>
              <span class="value">{{ selectedNode.approverValue || '-' }}</span>
            </div> -->
          </div>
        </el-dialog>
      </el-descriptions-item>
    </el-descriptions>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed, watch, nextTick } from 'vue'

// 定义props和emits
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  processData: {
    type: Object,
    default: () => ({})
  }
})

const emit = defineEmits(['update:visible', 'close'])

// 响应式数据
const selectedVersion = ref('V1.0')
const selectedNodeId = ref(null)
const flowDiagram = ref(null)
const nodeDetailVisible = ref(false)

// 版本数据
const versions = computed(() => {
  // 默认提供一个版本选项
  return [{ label: 'V1.0', value: 'V1.0' }]
})

// 计算选中的节点信息
const selectedNode = computed(() => {
  if (!selectedNodeId.value || !props.processData.nodes) return null
  return props.processData.nodes.find((node) => node.nodeId === selectedNodeId.value)
})

// 方法
const handleVersionChange = () => {
  selectedNodeId.value = null // 切换版本时清空选中的节点
}

const handleNodeClick = (node) => {
  selectedNodeId.value = node.nodeId
  nodeDetailVisible.value = true
}

// 关闭弹窗处理
// 格式化审批人类型
const formatApproverType = (type) => {
  const typeMap = {
    '01': '指定用户',
    '02': '部门负责人',
    '03': '角色',
  }
  return typeMap[type] || type || '-'
}

const handleClose = () => {
  // 重置选择的节点状态
  selectedNodeId.value = null
  nodeDetailVisible.value = false
  // 发送关闭事件给父组件
  emit('update:visible', false)
  emit('close')
}

// 更新visible状态
const updateVisible = (value) => {
  emit('update:visible', value)
}

// 监听弹窗显示状态
watch(
  () => props.visible,
  (newVal) => {
    if (newVal) {
      // 设置默认版本
      selectedVersion.value = props.processData.version || 'V1.0'
      selectedNodeId.value = null
      nodeDetailVisible.value = false
    } else {
      // 关闭主弹窗时同时关闭节点详情弹窗
      nodeDetailVisible.value = false
    }
  }
)
</script>

<style lang="scss" scoped>
/* 对话框样式 */

:deep(.el-dialog__title) {
  font-size: 16px;
  font-weight: 500;
}

:deep(.el-dialog__body) {
  padding: 20px;
  max-height: 70vh;
  overflow-y: auto;
}

:deep(.el-dialog__footer) {
  padding: 16px 20px;
  border-top: 1px solid #ebeef5;
  background-color: #fafafa;
}

/* 自定义描述列表样式 */
.custom-descriptions {
  width: 100%;
}

:deep(.custom-descriptions .el-descriptions__item) {
  display: flex;
  align-items: flex-start;
  margin-bottom: 10px;
}

:deep(.custom-descriptions .el-descriptions__label) {
  width: 140px;
  font-weight: 500;
  color: #606266;
  text-align: left;
  background-color: #f5f7fa;
  padding: 24px 20px;
  border-right: 1px solid #ebeef5;
  line-height: 1.5;
}

:deep(.custom-descriptions .el-descriptions__content) {
  flex: 1;
  color: #303133;
  padding: 24px 20px;
  background-color: #ffffff;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  line-height: 1.5;
}

/* 确保单元格边框正确显示 */
:deep(.custom-descriptions .el-descriptions__cell) {
  padding: 6px !important;
  border-bottom: 1px solid #ebeef5;
}

:deep(.custom-descriptions .el-descriptions__row:last-child .el-descriptions__cell) {
  border-bottom: none;
}

/* 流程图样式 */
.flow-diagram {
  width: 100%;
  border: 1px solid #e4e7ed;
  border-radius: 6px;
  padding: 40px 10px;
  background-color: #fff;
  overflow-x: auto;
  margin-bottom: 20px;
}

.flow-nodes {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  position: relative;
  gap: 80px;
  padding: 20px 0;
  flex-wrap: wrap;
}

.flow-node {
  display: flex;
  align-items: center;
  position: relative;
  cursor: pointer;

  &.start-node,
  &.end-node {
    .node-content {
      background-color: #67c23a;
      color: white;
      border-radius: 50%;
      padding: 8px 20px;
      font-size: 16px;
      min-width: 80px;
      text-align: center;
      width: 80px;
      height: 80px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-weight: 500;
      box-shadow: 0 2px 8px rgba(103, 194, 58, 0.3);
    }
    
    &.end-node .node-content {
      background-color: #909399;
      box-shadow: 0 2px 8px rgba(144, 147, 153, 0.3);
      border: 3px solid #e4e7ed;
    }
  }

  &.process-node {
    .node-content {
      background-color: #409eff;
      color: white;
      border-radius: 6px;
      padding: 16px 24px;
      font-size: 16px;
      min-width: 160px;
      text-align: center;
      transition: all 0.3s;
      border: 1px solid #e4e7ed;
      font-weight: 500;
      box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);

      &:hover {
        background-color: #337ecc;
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(64, 158, 255, 0.4);
      }
    }

    &.active .node-content {
      background-color: #f56c6c;
      box-shadow: 0 4px 12px rgba(245, 108, 108, 0.4);
      border-color: #f56c6c;
      transform: translateY(-2px);
    }
  }

  /* 节点箭头 */
  .node-arrow {
    position: absolute;
    right: -80px;
    top: 50%;
    transform: translateY(-50%);
    width: 80px;
    height: 0;
    border-bottom: 2px solid #409eff;
  }
  
  .node-arrow::after {
    content: '';
    position: absolute;
    right: 0;
    top: -4px;
    width: 0;
    height: 0;
    border-left: 8px solid #409eff;
    border-top: 6px solid transparent;
    border-bottom: 6px solid transparent;
  }
}

/* 节点详情样式 */
.node-detail-content {
  .info-row {
    margin-bottom: 16px;
    display: flex;
    align-items: center;
    padding: 8px 0;
    border-bottom: 1px solid #f5f7fa;

    &:last-child {
      border-bottom: none;
      margin-bottom: 0;
    }

    .label {
      font-weight: 500;
      color: #606266;
      min-width: 120px;
      font-size: 14px;
    }

    .value {
      color: #303133;
      font-size: 14px;
      flex: 1;
    }
  }
}

.dialog-footer {
  text-align: right;
}
</style>
